package com.examsystem.service.impl;


import  com.examsystem.dao.AnswerMapper;
import com.examsystem.dao.PaperMapper;
import com.examsystem.util.UploadImgUtils;
import com.examsystem.dao.QuestionMapper;
import com.examsystem.pojo.PictureQuestion;
import com.examsystem.pojo.Question;
import com.examsystem.service.QuestionService;
import com.examsystem.pojo.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.ws.rs.DefaultValue;
import java.util.List;

import static com.examsystem.Const.ConstMsg.*;


/**
 * @author fire'ge
 */
@Service("questionServiceImpl")
@Slf4j
public class QuestionServiceImpl implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private PaperMapper paperMapper;
    @Resource
    private AnswerMapper answerMapper;

    @Override
    public List<Question> queryQuestionsByPaperId(int paperId) {
        boolean flag = paperMapper.queryPaperById(paperId)==null;
        if (flag){
            throw new RuntimeException(CHECK_MSG_FAIL_MSG);
        }
        return questionMapper.queryQuestionsByPaperId(paperId);
    }
    @Override
    public List<Question> queryQuestionsByPaperIdAndUserId(int paperId,int userId) {
        boolean flag = paperMapper.queryPaperById(paperId)==null;
        if (flag){
            throw new RuntimeException(CHECK_MSG_FAIL_MSG);
        }
        return questionMapper.queryQuestionsByPaperIdAndUser(paperId,userId);
    }
    /*
    教师端
     */

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加试题
     * @param question  试题对象
     * @return  result结果集对象
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public Result addQuestion(Question question) {
        int i = questionMapper.insertQuestion(question);
        if (i >= NUMBER_1){
            String key = "question: "+question.getQuestionId();
            redisTemplate.opsForValue().set(key,question);
            return Result.succ("试题添加成功！【含有图片类型的答案/解析】");
        }
        return Result.fail("未添加成功");
    }

    /**
     * 给试题添加图片
     * @param imgA 答案图片
     * @param imgP 解析图片
     * @return result结果集对象
     */
    @Override
    public Result addQuestionPic(@DefaultValue("null") MultipartFile imgA,@DefaultValue("null") MultipartFile imgP,int questionId) {
        int i;String aImg;String pImg;
        synchronized (this.getClass()) {
            UploadImgUtils uploadImgUtils = new UploadImgUtils();
            PictureQuestion pictureQuestion = new PictureQuestion();
            aImg= uploadImgUtils.uploadImg(imgA);
            pImg = uploadImgUtils.uploadImg(imgP);
            pictureQuestion.setPictureRouteA(aImg);
            pictureQuestion.setPictureRouteP(pImg);
            pictureQuestion.setPictureQuestion(questionId);
            i = questionMapper.insertQuestionPic(pictureQuestion) + questionMapper.updateQuestionPic(questionId, pictureQuestion.getPictureId());
        } if (i >= NUMBER_2){
            String key1 = "questionPic1: "+questionId+aImg;
            String key2 = "questionPic2: "+questionId+pImg;
            redisTemplate.opsForValue().set(key1,aImg);
            redisTemplate.opsForValue().set(key2,pImg);
            return Result.succ("图片上传成功！");
        }
        return Result.fail("图片上传失败！");
    }



    /**
     * 整张试卷的所有questionId
     * @param paperId 试卷id
     * @return  list集合的questionId
     */
    @Override
    public List<Integer> showQuestionIds(int paperId) {
        String key = "paperQuestionId: "+paperId;
        Object questionIdsObj = redisTemplate.opsForValue().get(key);
        if (questionIdsObj == null){
            synchronized (this.getClass()){
                questionIdsObj = redisTemplate.opsForValue().get(key);
                if (questionIdsObj == null){
                    List<Integer> questionIds = answerMapper.showPaperQuestionId(paperId);
                    redisTemplate.boundListOps(key).leftPush(questionIds);
                    return questionIds;
                }
            }
        }else {
            return (List<Integer>) redisTemplate.boundListOps(key);
        }
        return null;
    }
}
